﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;

namespace Billing.Win
{
    public partial class ClientPaymentsForm : Billing.WindowsBase.FormBase
    {
        public ClientPaymentsForm()
        {
            InitializeComponent();
        }
        BillingDataContext mBillingDataContext;
        public BillingDataContext BillingDataContext
        {
            get
            {
                if (mBillingDataContext == null)
                    mBillingDataContext = new BillingDataContext();
                return mBillingDataContext;
            }
            set { mBillingDataContext = value; }
        }

        private int mClientID;

        public int ClientID
        {
            get { return mClientID; }
            set { mClientID = value; }
        }

        private void ClientPaymentsForm_Load(object sender, EventArgs e)
        {
            this.dtFrom.Value = DateTime.Now.AddMonths(-1);
            this.dtTo.Value = DateTime.Now;
           
            this.onSearch();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void onSearch()
        {
            this.bsPayments.DataSource = this.BillingDataContext.PaymentsSelectByPeriod(
                                                                    this.ClientID,
                                                                    null,
                                                                    null,
                                                                    (DateTime)this.dtFrom.Value,
                                                                    (DateTime)this.dtTo.Value);
            this.grdPayments.Rows.ExpandAll(false);
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.onSearch();
        }

        private void cmbMonthFrom_ValueChanged(object sender, EventArgs e)
        {
            if (tag == true)
                this.dtFrom.DateTime = new DateTime(this.dtFrom.DateTime.Year, (int)this.cmbMonthFrom.Value, 1);
        }

        private void cmbMonthTo_ValueChanged(object sender, EventArgs e)
        {
            if (tag == true)
                this.dtTo.DateTime = new DateTime(this.dtTo.DateTime.Year, (int)this.cmbMonthTo.Value, 1).AddMonths(1).AddDays(-1);
        }
        bool tag = true;
        private void dtFrom_ValueChanged(object sender, EventArgs e)
        {
            tag = false;
            this.cmbMonthFrom.Value = this.dtFrom.DateTime.Month;
            tag = true;
        }

        private void dtTo_ValueChanged(object sender, EventArgs e)
        {
            tag = false;
            this.cmbMonthTo.Value = this.dtTo.DateTime.Month;
            tag = true;
        }

    }
}
